package com.service.pay.diamond.service;

import com.alibaba.fastjson.JSONObject;
import com.service.pay.diamond.config.WebPayConfig;
import com.service.pay.diamond.dto.webpay.WxUnifiedOrderDto;
import com.service.pay.diamond.dto.webpay.WxUnifiedResultDto;
import com.tool.base.library.http.HttpModel;
import com.tool.base.library.http.HttpResponse;
import com.tool.base.library.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@Slf4j
public class WebPayService {
    @Autowired
    private WebPayConfig config;

    @Autowired
    private HttpUtil httpUtil;

    /*请求webpay微信下单*/
    public WxUnifiedResultDto unifiedOrder(WxUnifiedOrderDto wxUnifiedOrderDto) {
        HttpModel httpModel = new HttpModel();
        httpModel.setRequestBody(JSONObject.toJSONString(wxUnifiedOrderDto));
        try {
            HttpResponse response = httpUtil.postJson(config.getUnifiedOrderUrl(), httpModel);
            log.warn("{}", response.getBody());
            JSONObject jsonObject = JSONObject.parseObject(response.getBody());
            return jsonObject.getObject("data", WxUnifiedResultDto.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
